library(cfbfastR)
library(gt)
library(gtExtras)
library(data.table)
library(tidyverse)
library(magrittr)
library(janitor)
library(here)
gt_theme_f5 <- function(gt_object, ...) {
  
  gt_object %>%
    opt_table_font(
      font = list(
        google_font("Roboto"),
        default_fonts()
      ),
      weight = 400
    ) %>%
    tab_style(
      locations = cells_title("title"),
      style = cell_text(
        font = google_font("Roboto"),
        weight = 700
      )
    ) %>%
    tab_style(
      locations = cells_title("subtitle"),
      style = cell_text(
        font = google_font("Roboto"),
        color = "gray65",
        weight = 400
      )
    ) %>%
    tab_style(
      style = list(
        cell_borders(
          sides = "top", color = "black", weight = px(0)
        ),
        cell_text(
          font = google_font("Roboto"),
          #transform = "uppercase",
          v_align = "bottom",
          size = px(14),
          weight = 'bold'
        )
      ),
      locations = list(
        gt::cells_column_labels(),
        gt::cells_stubhead()
      )
    ) %>%
    tab_options(
      column_labels.background.color = "floralwhite",
      data_row.padding = px(7.5),
      heading.border.bottom.style = "none",
      table.border.top.style = "none", # transparent
      table.border.bottom.style = "none",
      column_labels.font.weight = "bold", 
      column_labels.border.top.style = "none",
      column_labels.border.bottom.width = px(2),
      column_labels.border.bottom.color = "black",
      row_group.border.top.style = "none",
      row_group.border.top.color = "black",
      row_group.border.bottom.width = px(1),
      row_group.border.bottom.color = "floralwhite",
      stub.border.color = "floralwhite",
      stub.border.width = px(0),
      source_notes.font.size = 12,
      source_notes.border.lr.style = "none",
      table.font.size = 16,
      heading.align = "left",
      table.background.color = "floralwhite",
      table_body.hlines.color = 'gray90',
      ...
    )
}
stats <- cfbfastR::cfbd_stats_season_player(2024,
                                   season_type = "both",
                                   conference = "MWC") 


rush <- stats |>
  filter(rushing_yds > 500) |>
  mutate(passing_completions = ifelse(is.na(passing_completions), 0, passing_completions)) |>
  filter(passing_completions < 5) |>
  select(team, athlete_id, player, rushing_car, rushing_yds, rushing_td, rushing_long, rushing_ypc, fumbles_lost, fumbles_fum, receiving_rec, receiving_yds, receiving_td, receiving_long)
  

usage <- cfbd_player_usage(2024,
                            conference = "MWC")

rb_usage <- usage |>
  filter(position == "RB") |>
  select(athlete_id, usg_overall, usg_rush, usg_pass)


info <- cfbd_team_info()
## New names:
## 2025-06-07 10:52:48.513685:Invalid arguments or no team data available!
## • `id` -> `id...1`
## • `id` -> `id...14`
rush %<>%
  left_join(info %>% select(school, logo), by = c("team" = "school"))
rbs <- fread(here("data", "2025 MW RBs.csv")) %>%
  clean_names() %>%
  mutate(rank = row_number()) %>%
  arrange(rank) %>%
  mutate(rank = paste0(row_number(), ".")) %>%
  select(rank, everything()) %>%
  mutate(yards = as.numeric(str_remove_all(yards, "[,\\s]"))) %>%
  filter(!is.na(yards)) %>%
  mutate(school = ifelse(school == "San Jose State", "San José State", school)) %>%
  left_join(info %>% select(school, logo), by = c("school" = "school"))
rbs %>%
  select(rank, logo, player, yards, yards_per_attempt, pff_rush_grade, missed_tackles_forced) %>%
  gt() %>%
  gt_theme_f5() %>%
  gt_img_rows(
    columns = logo,
    height = 30
  ) %>%
  tab_header(
    title = "Mountain West Running Backs",
    subtitle = "Top 15"
  ) %>%
  cols_label(
    logo = "",
    rank = "Rank",
    player = "Player",
    yards = "Yards",
    yards_per_attempt = "YPA",
    pff_rush_grade = "PFF Rush Grade",
    missed_tackles_forced = "Missed Tackles Forced"
  ) %>%
  data_color(yards, palette = "rcartocolor::Tropic", domain = c(100, 1400), reverse = T) %>%
  data_color(yards_per_attempt, palette = "rcartocolor::Tropic", domain = c(3.5, 8), reverse = T) %>%
  data_color(pff_rush_grade, palette = "rcartocolor::Tropic", domain = c(60, 95), reverse = T) %>%
  tab_options(data_row.padding = '0px')
Mountain West Running Backs
Top 15
Rank Player Yards YPA PFF Rush Grade Missed Tackles Forced
1. Jai'Den Thomas 918 5.6 83.5 31
2. Floyd Chalk IV 735 4.8 81.0 45
3. Sire Gaines 152 7.6 66.8 6
4. Scottre Humphrey 1385 6.9 92.0 44
5. Malik Sherrod 967 5.6 81.8 34
6. Justin Marshall 753 4.9 78.8 33
7. Bryson Donelson 476 6.1 81.1 17
8. Dylan Carson 600 4.3 70.1 11
9. Jaylon Glover 562 4.1 73.0 26
10. Christian Washington 536 5.3 81.3 23
11. Herschel Turner 407 5.1 75.1 19
12. Sam Scott 435 4.7 76.4 17
13. Landon Sims 351 4.9 70.8 17
14. Elijah Gilliam 457 3.8 72.1 18
15. Damon Bankston 1115 6.2 80.0 52